import { Injectable } from '@angular/core';
import { ACLService } from '@delon/acl';

@Injectable()
export class PermissionService {

  constructor(
    private aclService: ACLService
  ) {

  }

  /**
     * 添加权限名称
     * @param permissionName 添加(单个/多个)权限名称
     */
  addPermission(permissionName: string | string[]) {
    if (!permissionName || permissionName.length === 0) {
      return;
    }

    if (Array.isArray(permissionName)) {
      for (let i = 0; i < permissionName.length; i++) {
        let tmppermissionName = permissionName[i];
        if (this.aclService.data.abilities.find(item => item === tmppermissionName)) {
          continue;
        }
        this.aclService.attachRole([tmppermissionName]);
      }
    } else {
      if (this.aclService.data.abilities.find(item => item === permissionName)) {
        return;
      }
      this.aclService.attachRole([permissionName]);
    }
  }

  /**
  * 填充数据
  * @param auth
  */
  extend(auth) {

    let permissions: string[] = [];
    for (let permission in auth.grantedPermissions) {
      permissions.push(permission);
    };
    this.addPermission(permissions)
  }


}
